package com.lion.seaman.common.tools;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class URLTools {
	
	public static Map<String, String> getParams(String param,boolean needDecoder) {
		Map<String, String> map = new HashMap<String, String>();
		if(param == null){
			return map;
		}
		if(needDecoder){
			try {
				param = URLDecoder.decode(param, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		String[] url = param.split("\\?");
		for(String paramString : url){
			parseURL(paramString,map);
		}
		return map;
	}
	
	public static void parseURL(String params,Map<String, String> map){
		String[] paramsArr = params.split("\\&");
		for (String kv : paramsArr) {
			String[] keyValue = kv.split("=");
			if (keyValue.length == 2) {
				map.put(keyValue[0], keyValue[1]);
			} else {
				map.put(keyValue[0], "");
			}
		}
	}
	
}
